/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.dataaccess.hibernate;

import cassys.dataaccess.AssessmentDAO;
import cassys.datatransferobjects.AssessmentDTO;
import cassys.datatransferobjects.AssessmentItemsDTO;
import java.util.ArrayList;
import cassys.exceptions.HibernateAccessLayerException;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Priyanka
 */
public class AssessmentDAOImpl  extends AbstractDao implements AssessmentDAO {

    @Override
    public void insertAssessment(AssessmentDTO dto) {
        Logger.getLogger(getClass().getName()).log(Level.INFO, "Inserting assessment " + dto.getAssessmentId() );
     try {
            super.saveOrUpdate(dto);
           List<AssessmentItemsDTO> items = new ArrayList<AssessmentItemsDTO>();
               items=   dto.getAssessmentItems();
            for (int i =0; i< items.size(); i++) {
                insertAssessmentItem(items.get(i));
            }
        
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    }

    @Override
    public void updateAssessment(AssessmentDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteAssessment(AssessmentDTO dto) {
        super.delete(dto);
    }

    @Override
    public AssessmentDTO findAssessment(String assessmentId) {
        AssessmentDTO assess = null;
        try {
            assess = (AssessmentDTO) super.find(AssessmentDTO.class, assessmentId);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
        return assess;
    }

    @Override
    public ArrayList<AssessmentDTO> findAllAssessment() {
        try {
            return (ArrayList<AssessmentDTO>) super.findAll(AssessmentDTO.class);
            } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
    }
    
     @Override
    public ArrayList<AssessmentDTO> findAllAssessmentById(String assessmentId) {
        try {
            return (ArrayList<AssessmentDTO>) super.findAllByParam(AssessmentDTO.class, "assessmentId", assessmentId);
            } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
    }
    
    public AssessmentDTO[] getAllAssessmentByModule(String moduleCode) {
        
         try {  
             
           List ls = super.findByCategory(AssessmentDTO.class,"moduleCode",moduleCode);
           AssessmentDTO[] list = new AssessmentDTO[ls.size()];
           ls.toArray(list);
           return list;
             
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
        
    
    }
    
    
    @Override
    public void insertAssessmentItem(AssessmentItemsDTO dto) {
        try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Inserting assessment item " + dto.getAssessmentItemId()+" for Assessmentid "+dto.getAssessmentId());
            super.saveOrUpdate(dto);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    }
    
    

    @Override
    public void deleteAssessmentItem(String assessmentItemId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void updateAssessmentItem(AssessmentItemsDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AssessmentItemsDTO findAssessmentItems(String assessmentItemId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<AssessmentItemsDTO> findAllAssessmentItems() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
